package com.yanqu.road.server.manger.activity.qixi.task;

import com.yanqu.road.logic.pb.PlayerBasePb;
import com.yanqu.road.pb.activity.QixiProto;
import com.yanqu.road.server.GameServer;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.qixi.QixiModule;
import com.yanqu.road.server.manger.activity.qixi.QixiMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.player.UserMgr;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.executor.GeneralTask;

public class QixiReceiveFlowerTask extends GeneralTask {
    private long sendSeverId;
    private long sendUserId;
    private long targetId;
    private int flowerId;
    private int flowerCount;
    private String content;

    public QixiReceiveFlowerTask(long sendServerId, long sendUserId, long targetId, int flowerId, int flowerCount, String content) {
        this.sendSeverId = sendServerId;
        this.sendUserId = sendUserId;
        this.targetId = targetId;
        this.flowerId = flowerId;
        this.flowerCount = flowerCount;
        this.content = content;
    }

    @Override
    public void doRun() {
        GamePlayer player = GamePlayerMgr.getPlayer(targetId);
        if (null != player) {
            QixiProto.CrossQixiSendFlowerRespMsg.Builder resultMsg = player.getModule(QixiModule.class).receiveFlower(sendUserId, flowerId, flowerCount, content);

            QixiProto.CrossQixiSendFlowerRespMsg.Builder backMsg = QixiProto.CrossQixiSendFlowerRespMsg.newBuilder();
            backMsg.setRet(resultMsg.getRet());
            backMsg.setRemainCanReceive(resultMsg.getRemainCanReceive());
            backMsg.setFlowerId(flowerId);
            backMsg.setFlowerCount(flowerCount);
            backMsg.setTargetUserId(targetId);
            backMsg.setSendUserId(sendUserId);
            if (resultMsg.getRet() == 0) {
                backMsg.setTargetUserId(targetId);
                backMsg.setUserBaseInfo(PlayerBasePb.parsePlayerBaseTempMsg(UserMgr.parseUserBaseInfo(player.getUserInfo(), GameServer.getInstance().getServerId())));
                backMsg.setFlowerId(flowerId);
                backMsg.setFlowerCount(flowerCount);
                backMsg.setContent(content);
                backMsg.setSendUserId(sendUserId);
            }

            if (sendSeverId == GameServer.getInstance().getServerId()) {
                GamePlayer backPlayer = GamePlayerMgr.getPlayer(sendUserId);
                if (backPlayer != null) {
                    backPlayer.getModule(QixiModule.class).sendFlowerSuccessBack(backMsg.build());
                }
            } else {
                GamePlayerMgr.sendToGameServer(sendSeverId, GameProtocol.S_QIXI_SEND_FLOWER_ASYNC_BACK, player.getUserId(), backMsg, null);
            }
        } else {
            QixiProto.CrossQixiSendFlowerRespMsg.Builder backMsg = QixiProto.CrossQixiSendFlowerRespMsg.newBuilder();
            backMsg.setRet(GameErrorCode.E_USER_NO_FOUND);
            backMsg.setFlowerId(flowerId);
            backMsg.setFlowerCount(flowerCount);
            backMsg.setTargetUserId(targetId);
            backMsg.setSendUserId(sendUserId);
            GamePlayerMgr.sendToGameServer(sendSeverId, GameProtocol.S_QIXI_SEND_FLOWER_ASYNC_BACK, targetId, backMsg, null);
        }
    }
}
